using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using FinanceStats.Entities;
using System.Collections;
using FinanceStats.Business;
using FinanceStats.Business.Components;
using FinanceStats.Common;
using System.IO;
using FinanceStats.ReportingService;
using Informatika.Common;

namespace FinanceStats
{
    public partial class MainForm : Form
    {
        private int userID = -1;
        
        private List<OutcomeGroup> outcomeGroups = null;
        private List<OutcomeGroup> outcomeGorupsTop = null;
        private Dictionary<int, List<OutcomeItem>> outcomeItems = null;
        private UserOutcome userOutcome = null;
        private Euro euro = null;

        private EditGroup editGroupDialog = null;
        private EditItem editItemDialog = null;
        private Resolver resolverDialog = null;
        private OutcomeDayControl outocmeDayControl = null;
        private OutcomeWeekControl outcomeWeekControl = null;
        private OutcomeMonthConotrol outcomeMonthControl = null;

        private TotalReportForm trForm = null;

        private DisplayMode displayMode = DisplayMode.daily;

        private bool raiseDateChanged = true;

        private NbsExchangeRateService.TempExchangeRatesService exchangeService;
        private NbsExchangeRateService.AuthenticationHeader aHeader;

        public MainForm()
        {
            InitializeComponent();
            exchangeService = new FinanceStats.NbsExchangeRateService.TempExchangeRatesService();
            aHeader = new FinanceStats.NbsExchangeRateService.AuthenticationHeader();
            aHeader.UserName = AppSettingsHelper.GetWebConfigValue(Constants.NbsUserName);
            aHeader.Password = AppSettingsHelper.GetWebConfigValue(Constants.NbsPassword);
            aHeader.LicenceID = Int64.Parse(AppSettingsHelper.GetWebConfigValue(Constants.NbsLicenceID));
        }

        private void InitializeStartupdata()
        {
            outcomeGorupsTop = DataManager.Instance.LoadTopMonthlyGroups(10, DateTime.Today);
            BindingSource bs = new BindingSource();
            bs.DataSource = outcomeGorupsTop;

            this.dataGridView1.DataSource = bs;
            this.dataGridView1.Columns["GroupID"].Visible = false;
            this.dataGridView1.Columns["GroupTotal"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            this.dataGridView1.Columns["GroupID"].ReadOnly = true;
            this.dataGridView1.Columns["GroupTotal"].ReadOnly = true;
            this.dataGridView1.AllowUserToAddRows = false;
            this.dataGridView1.AllowUserToOrderColumns = false;
            this.dataGridView1.AllowUserToResizeColumns = false;
            this.dataGridView1.AllowUserToResizeRows = false;
            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            this.dataGridView1.RowHeadersVisible = false;

            List<Importitem> importItems = new List<Importitem>();
            BindingSource bs1 = new BindingSource();
            bs1.DataSource = importItems;

            this.dataGridNotMatched.DataSource = bs1;
            this.dataGridNotMatched.Columns["Importitemid"].Visible = false;
            this.dataGridNotMatched.AllowUserToAddRows = false;
            this.dataGridNotMatched.AllowUserToOrderColumns = false;
            this.dataGridNotMatched.AllowUserToResizeColumns = false;
            this.dataGridNotMatched.AllowUserToResizeRows = false;
            this.dataGridNotMatched.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.dataGridNotMatched.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            this.dataGridNotMatched.RowHeadersVisible = false;
            this.dataGridNotMatched.Columns["Date"].DisplayIndex = 0;
            this.dataGridNotMatched.Columns["Itemname"].DisplayIndex = 1;
            this.dataGridNotMatched.Columns["Amount"].DisplayIndex = 2;

            this.dataGridNotMatched.Columns["Date"].DefaultCellStyle.Format = "dd-MM-yyyy";
            this.dataGridNotMatched.Columns["Amount"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

            List<UserOutcome> outcomeItems = new List<UserOutcome>();
            BindingSource bs2 = new BindingSource();
            bs2.DataSource = outcomeItems;

            this.dataGridMatched.DataSource = bs2;
            this.dataGridMatched.Columns["UserOutcomeID"].Visible = false;
            this.dataGridMatched.Columns["GroupID"].Visible = false;
            this.dataGridMatched.Columns["ItemID"].Visible = false;
            this.dataGridMatched.Columns["UserID"].Visible = false;

            this.dataGridMatched.AllowUserToAddRows = false;
            this.dataGridMatched.AllowUserToOrderColumns = false;
            this.dataGridMatched.AllowUserToResizeColumns = false;
            this.dataGridMatched.AllowUserToResizeRows = false;
            this.dataGridMatched.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.dataGridMatched.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            this.dataGridMatched.RowHeadersVisible = false;
            this.dataGridMatched.Columns["Date"].DisplayIndex = 0;
            this.dataGridMatched.Columns["ItemName"].DisplayIndex = 1;
            this.dataGridMatched.Columns["Amount"].DisplayIndex = 2;

            this.dataGridMatched.Columns["Date"].DefaultCellStyle.Format = "dd-MM-yyyy";
            this.dataGridMatched.Columns["Amount"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

            ImportManager.Instance.DataImported += new DataImportedHandler(Instance_DataImported);
            trForm = new TotalReportForm(this.userID);

            this.euro = new Euro();
            this.euroTxt.Text = DataManager.Instance.LoadEuroCourseDay(dateTimePicker1.Value.Date).ToString();
        }

        private void LoadCashe()
        {
            outcomeGroups = DataManager.Instance.LoadOutcomeGroups();
            if (outcomeGroups != null)
            {
                outcomeItems = new Dictionary<int, List<OutcomeItem>>();
                foreach (OutcomeGroup og in outcomeGroups)
                {
                    List<OutcomeItem> items = DataManager.Instance.LoadOutcomeItems((int)og.GroupID);
                    outcomeItems.Add((int)og.GroupID, items);
                }
            }
        }

        void  Instance_DataImported(List<Importitem> list)
        {
            List<UserOutcome> matched = ImportManager.Instance.MatchData(ref list, this.userID);

            ((BindingSource)dataGridMatched.DataSource).DataSource = matched;
            ((BindingSource)dataGridNotMatched.DataSource).DataSource = list;

        }

        private void ShowLoginDialog()
        {
            Login l = new Login();
            if (l.ShowDialog(this) == DialogResult.Cancel)
            {
                this.Close();
            }
            else
            {
                this.Text += " - " + l.Username;
                this.userID = l.UserID;
            }
        }

        private void ApplyBindings()
        {
            if (this.outcomeGroups != null)
            {
                BindingSource bs = new BindingSource();
                bs.DataSource = this.outcomeGroups;
                cmbGroup.DataSource = bs;
                cmbGroup.DisplayMember = "GroupName";
                cmbGroup.ValueMember = "GroupID";
                cmbGroup.SelectedIndexChanged += new System.EventHandler(this.cmbGroup_SelectedIndexChanged);
                int? groupId = (int?)cmbGroup.SelectedValue;
                RebindItems(groupId);
            }
        }

        private void RebindItems(int? GroupID)
        {
            BindingSource bs = new BindingSource();
            if (GroupID != null)
            {
                bs.DataSource = this.outcomeItems[(int)GroupID];
                cmbItem.DataSource = bs;
                cmbItem.DisplayMember = "ItemName";
                cmbItem.ValueMember = "ItemID";
                ((BindingSource)cmbItem.DataSource).ResetBindings(false);
            }
            else
            {
                bs.DataSource = null;
            }
        }

        private void AddOutcomeItem()
        {
            OutcomeItem oi = new OutcomeItem();
            oi.GroupID = (int?)cmbGroup.SelectedValue;
            EditItemDialog.OutcomeGroups = this.outcomeGroups;
            EditItemDialog.OutcomeItem = oi;
            EditItemDialog.ApplyBindings();
            DialogResult dres = EditItemDialog.ShowDialog();
            if (dres == DialogResult.No)
            {
                MessageBox.Show(this, "Error saving group", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                this.outcomeItems[(int)oi.GroupID] = DataManager.Instance.LoadOutcomeItems((int)oi.GroupID);
                BindingSource bs = new BindingSource();
                bs.DataSource = this.outcomeItems[(int)oi.GroupID];
                cmbItem.DataSource = bs;
                cmbItem.DisplayMember = "ItemName";
                cmbItem.ValueMember = "ItemID";
                ((BindingSource)cmbItem.DataSource).ResetBindings(false);
                if (oi.ItemID != null)
                {
                    cmbItem.SelectedValue = oi.ItemID;
                }
            }
        }

        private void LoadOutcome()
        {
            switch (displayMode)
            {
                case DisplayMode.daily: LoadDaily(); break;
                case DisplayMode.weekly: LoadWeekly(); break;
                case DisplayMode.monthly: LoadMonthly(); break;
            }
        }

        private void LoadDaily()
        {
            this.displayMode = DisplayMode.daily;
            groupBox2.Controls.Clear();
            DateTime date = dateTimePicker1.Value;
            OutcomeDay od = DataManager.Instance.LoadOutcomeDay(date, this.userID);
            OutocmeDayControl.OutcomeDay = od;
            groupBox2.Controls.Add(OutocmeDayControl);
        }

        private void LoadWeekly()
        {
            this.displayMode = DisplayMode.weekly;
            groupBox2.Controls.Clear();
            DateTime date = dateTimePicker1.Value;
            List<OutcomeDay> week = DataManager.Instance.LoadOutcomeWeek(date, this.userID);
            OutcomeWeekControl.OutcomeWeek = week;
            groupBox2.Controls.Add(OutcomeWeekControl);
        }

        private void LoadMonthly()
        {
            this.displayMode = DisplayMode.monthly;
            groupBox2.Controls.Clear();
            DateTime date = dateTimePicker1.Value;
            List<OutcomeDay> month = DataManager.Instance.LoadOutcomeMonth(date, this.userID);
            OutcomeMonthControl.OutcomeMonth = month;
            OutcomeMonthControl.Dock = DockStyle.Fill;
            groupBox2.Controls.Add(outcomeMonthControl);
        }

        private void SetCommonComponents(Resolver res)
        {
            res.Controls.Add(this.label1);
            res.Controls.Add(this.label2);
            res.Controls.Add(this.cmbGroup);
            res.Controls.Add(this.cmbItem);
            res.Controls.Add(this.button1);
            res.Controls.Add(this.button2);
            res.Controls.Add(this.button3);
            res.Controls.Add(this.button4);
            this.dataGridNotMatched.Location = new Point(250, 12);
            res.Controls.Add(this.dataGridNotMatched);

        }

        private void ResetCommonComponents()
        {
            this.groupBox1.Controls.Add(this.label1);
            this.groupBox1.Controls.Add(this.label2);
            this.groupBox1.Controls.Add(this.cmbGroup);
            this.groupBox1.Controls.Add(this.cmbItem);
            this.groupBox1.Controls.Add(this.button1);
            this.groupBox1.Controls.Add(this.button2);
            this.groupBox1.Controls.Add(this.button3);
            this.groupBox1.Controls.Add(this.button4);
            this.dataGridNotMatched.Location = new Point(316, 33);
            this.Controls.Find("groupBox5", true)[0].Controls.Add(this.dataGridNotMatched);
        }

        private void SetValues()
        {
            this.userOutcome.ItemID = (int?)cmbItem.SelectedValue;
            this.userOutcome.Date = dateTimePicker1.Value.Date;
            this.userOutcome.Amount = Convert.ToDecimal(textBox1.Text);
        }

        public EditGroup EditGroupDialog
        {
            get
            {
                if (editGroupDialog == null)
                {
                    editGroupDialog = new EditGroup();
                }
                return editGroupDialog;
            }
        }

        public EditItem EditItemDialog
        {
            get
            {
                if (editItemDialog == null)
                {
                    editItemDialog = new EditItem();
                }

                return editItemDialog;
            }
        }

        public Resolver ResolverDialog
        {
            get
            {
                if (resolverDialog == null)
                {
                    resolverDialog = new Resolver(this.userID);
                }
                return resolverDialog;
            }
        }


        public OutcomeDayControl OutocmeDayControl
        {
            get
            {
                if (outocmeDayControl == null)
                {
                    outocmeDayControl = new OutcomeDayControl();
                    outocmeDayControl.Dock = DockStyle.Fill;
                    outocmeDayControl.OutcomeItemChanged += new OutcomeItemChangedHandler(outocmeDayControl_OutcomeItemChanged);
                    outocmeDayControl.OnRemoving += new OutcomeItemChangedHandler(outocmeDayControl_OnRemoving);
                }
                return outocmeDayControl;
            }
        }

        public OutcomeMonthConotrol OutcomeMonthControl
        {
            get
            {
                if (outcomeMonthControl == null)
                {
                    outcomeMonthControl = new OutcomeMonthConotrol();
                    outcomeMonthControl.OutcomeDayDblClicked += new OutcomeDayDblClickedHandler(outcomeWeekControl_OutcomeDayDblClicked);
                }
                return outcomeMonthControl;
            }
        }

        void outocmeDayControl_OnRemoving(UserOutcome uo)
        {
            if (!DataManager.Instance.DeleteUserOutcome(uo))
            {
                MessageBox.Show("Error deleting outcome entry", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                LoadOutcome();
            }
        }

        public OutcomeWeekControl OutcomeWeekControl
        {
            get
            {
                if (outcomeWeekControl == null)
                {
                    outcomeWeekControl = new OutcomeWeekControl();
                    outcomeWeekControl.Dock = DockStyle.Fill;
                    outcomeWeekControl.OutcomeDayDblClicked += new OutcomeDayDblClickedHandler(outcomeWeekControl_OutcomeDayDblClicked);
                }
                return outcomeWeekControl;
            }
        }

        void outcomeWeekControl_OutcomeDayDblClicked(OutcomeDay day)
        {
            this.displayMode = DisplayMode.daily;
            groupBox2.Controls.Clear();
            OutocmeDayControl.OutcomeDay = day;
            raiseDateChanged = false;
            dateTimePicker1.Value = day.Date;
            raiseDateChanged = true;
            groupBox2.Controls.Add(OutocmeDayControl);
            radioButton1.Checked = true;
        }

        private void outocmeDayControl_OutcomeItemChanged(UserOutcome uo)
        {
            this.userOutcome = uo;
            cmbGroup.SelectedValue = this.userOutcome.GroupID;
            cmbItem.SelectedValue = this.userOutcome.ItemID;
            textBox1.Text = uo.Amount.ToString();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            ShowLoginDialog();
            InitializeStartupdata();
            LoadCashe();
            ApplyBindings();
            LoadOutcome();
            dateTimePicker1.ValueChanged += new EventHandler(dateTimePicker1_ValueChanged);
        }

        void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            if (raiseDateChanged)
            {
                LoadDaily();
            }
            this.euroTxt.Text = (string)DataManager.Instance.LoadEuroCourseDay(dateTimePicker1.Value.Date).ToString();
        }

        private void cmbGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox cmb = sender as ComboBox;
            int? groupId = (int?)cmb.SelectedValue;
            RebindItems(groupId);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OutcomeGroup og = (OutcomeGroup)cmbGroup.SelectedItem;
            EditGroupDialog.OutcomeGroup = og;
            DialogResult dres = EditGroupDialog.ShowDialog();
            if (dres == DialogResult.No)
            {
                MessageBox.Show(this, "Error savin group", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                ((BindingSource)cmbGroup.DataSource).ResetBindings(false);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OutcomeGroup og = new OutcomeGroup();
            EditGroupDialog.OutcomeGroup = og;
            DialogResult dres = EditGroupDialog.ShowDialog();
            if (dres == DialogResult.No)
            {
                MessageBox.Show(this, "Error savin group", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                if (og.GroupID != null)
                {
                    this.outcomeGroups.Add(og);
                    this.outcomeItems.Add((int)og.GroupID, new List<OutcomeItem>());
                    ((BindingSource)cmbGroup.DataSource).ResetBindings(false);
                    cmbGroup.SelectedItem = og;
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            OutcomeItem oi = cmbItem.SelectedItem as OutcomeItem;
            if (oi != null)
            {
                EditItemDialog.OutcomeGroups = this.outcomeGroups;
                EditItemDialog.OutcomeItem = oi;
                EditItemDialog.ApplyBindings();

                int? outcomeGroupId = oi.GroupID;
                DialogResult dres = EditItemDialog.ShowDialog();
                if (dres == DialogResult.No)
                {
                    MessageBox.Show(this, "Error saving group", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    if (oi.GroupID != outcomeGroupId)
                    {
                        this.outcomeItems[(int)outcomeGroupId] = DataManager.Instance.LoadOutcomeItems((int)outcomeGroupId);
                        this.outcomeItems[(int)oi.GroupID] = DataManager.Instance.LoadOutcomeItems((int)oi.GroupID);
                    }
                    ((BindingSource)cmbGroup.DataSource).ResetBindings(false);
                    ((BindingSource)cmbItem.DataSource).ResetBindings(false);
                    cmbGroup.SelectedValue = oi.GroupID;
                    cmbItem.SelectedValue = oi.ItemID;
                }
            }
            else
            {
                AddOutcomeItem();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            AddOutcomeItem();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.userOutcome = new UserOutcome();
            userOutcome.UserID = this.userID;
            userOutcome.Date = dateTimePicker1.Value.Date;
            userOutcome.ItemID = (int?)cmbItem.SelectedValue;
            textBox1.Text = userOutcome.Amount.ToString();
            
        }

        private void button6_Click(object sender, EventArgs e)
        {
            bool res = true;
            SetValues();
            if (this.userOutcome.UserOutcomeID == null)
            {
                res = DataManager.Instance.InsertUserOutcome(this.userOutcome);
            }
            else
            {
                res = DataManager.Instance.UpdateUserOutcome(this.userOutcome);
            }
            LoadOutcome();
        }

        private void radioButton_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                LoadDaily();
            }
            if (radioButton2.Checked)
            {
                LoadWeekly();
            }
            if (radioButton3.Checked)
            {
                LoadMonthly();
            }
        }

        private void button7_Click_1(object sender, EventArgs e)
        {
            this.outcomeGorupsTop = DataManager.Instance.LoadTopMonthlyGroups(10, dateTimePicker1.Value);
            ((BindingSource)this.dataGridView1.DataSource).DataSource = outcomeGorupsTop;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            dateTimePicker1.Value = dateTimePicker1.Value.AddDays(-1);
        }

        private void button9_Click(object sender, EventArgs e)
        {
            dateTimePicker1.Value = dateTimePicker1.Value.AddDays(1);
        }

        private void button10_Click(object sender, EventArgs e)
        {
            trForm.ShowDialog(this);
        }

        private void button11_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog(this) == DialogResult.OK && openFileDialog1.FileName != "")
            {
                ImportManager.Instance.ImportFromXLS(openFileDialog1.FileName);
            }
        }

        private void button13_Click(object sender, EventArgs e)
        {
            List<UserOutcome> outcomeItems = ((BindingSource)dataGridMatched.DataSource).DataSource as List<UserOutcome>;
            if (outcomeItems.Count > 0)
            {
                bool sucess = DataManager.Instance.InsertUserOutcomeList(outcomeItems);
                if (sucess)
                {
                    ((BindingSource)dataGridMatched.DataSource).Clear();
                    LoadDaily();
                }
                else
                {
                    MessageBox.Show(this, "Error saving usert outcome items", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void button12_Click(object sender, EventArgs e)
        {
            List<Importitem> items = ((BindingSource)dataGridNotMatched.DataSource).DataSource as List<Importitem>;
            if (items.Count > 0)
            {
                bool sucess = DataManager.Instance.SaveImportedItemsList(items);
                if (!sucess)
                {
                    MessageBox.Show(this, "Error saving imorted items", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void button14_Click(object sender, EventArgs e)
        {
            List<Importitem> allItems = DataManager.Instance.LoadImportItemsList();
            ((BindingSource)dataGridNotMatched.DataSource).DataSource = allItems;
            
        }

        private void button15_Click(object sender, EventArgs e)
        {
            Resolver res = this.ResolverDialog;
            SetCommonComponents(res);
            res.ShowDialog();
            ResetCommonComponents();
            LoadDaily();
        }
    
        private void button16_Click(object sender, EventArgs e)
        {
            if (!AppSettingsHelper.WebConfigValueExists(Constants.ExcelPath))
            {
                MessageBox.Show("This option is only enabled when Excel path is configured");
            }
            else
            {
                ImportManager.Instance.GenerateImportTemplate();
            }
        }

        private void button17_Click(object sender, EventArgs e)
        {

        }

        private void radioButtonGraphMode_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rb = sender as RadioButton;
            if (rb == radioButton4)
            {
                SetWeekly();
            }
            else if (rb == radioButton5)
            {
                SetMonthly();
            }
            else if (rb == radioButton6)
            {
                SetYearly();
            }
            else if (rb == radioButton7)
            {
                SetEver();
            }
            else
            {
                TogglePickers(true);
            }
        }

        private void SetWeekly()
        {
            TogglePickers(false);
            DateTime today = DateTime.Today;
            while (today.DayOfWeek != DayOfWeek.Monday)
            {
                today = today.AddDays(-1);
            }
            dateTimePicker2.Value = today;
            dateTimePicker3.Value = today.AddDays(6);
            List<OutcomeDay> costData = DataManager.Instance.LoadOutcomeRange(dateTimePicker2.Value, dateTimePicker3.Value, this.userID);
            Dictionary<DateTime, Decimal?> euroCourse = DataManager.Instance.LoadEuroCourse(dateTimePicker2.Value, dateTimePicker3.Value);
            this.totalCostGraphics1.CostData = costData;
            this.totalCostGraphics1.EuroCourseList = euroCourse;
        }

        private void SetMonthly()
        {
            TogglePickers(false);
            DateTime today = DateTime.Today;
            while (today.Day != 1)
            {
                today = today.AddDays(-1);
            }
            dateTimePicker2.Value = today;
            dateTimePicker3.Value = today.AddMonths(1).AddDays(-1);
            List<OutcomeDay> costData = DataManager.Instance.LoadOutcomeRange(dateTimePicker2.Value, dateTimePicker3.Value, this.userID);
            Dictionary<DateTime, Decimal?> euroCourse = DataManager.Instance.LoadEuroCourse(dateTimePicker2.Value, dateTimePicker3.Value);

            this.totalCostGraphics1.CostData = costData;
            this.totalCostGraphics1.EuroCourseList = euroCourse;

        }

        private void SetYearly()
        {
            TogglePickers(false);
            DateTime today = DateTime.Today;
            while (today.DayOfYear != 1)
            {
                today = today.AddDays(-1);
            }
            dateTimePicker2.Value = today;
            dateTimePicker3.Value = today.AddYears(1).AddDays(-1);
            List<OutcomeDay> costData = DataManager.Instance.LoadOutcomeRange(dateTimePicker2.Value, dateTimePicker3.Value, this.userID);
            Dictionary<DateTime, Decimal?> euroCourse = DataManager.Instance.LoadEuroCourse(dateTimePicker2.Value, dateTimePicker3.Value);

            this.totalCostGraphics1.CostData = costData;
            this.totalCostGraphics1.EuroCourseList = euroCourse;

        }

        private void SetEver()
        {
            TogglePickers(false);
            dateTimePicker2.Value = DataManager.Instance.GetFirstDate(userID);
            dateTimePicker3.Value = DataManager.Instance.GetLastDate(userID);
            List<OutcomeDay> costData = DataManager.Instance.LoadOutcomeRange(dateTimePicker2.Value, dateTimePicker3.Value, this.userID);
            Dictionary<DateTime, Decimal?> euroCourse = DataManager.Instance.LoadEuroCourse(dateTimePicker2.Value, dateTimePicker3.Value);

            this.totalCostGraphics1.CostData = costData;
            this.totalCostGraphics1.EuroCourseList = euroCourse;

        }

        private void TogglePickers(bool value)
        {
            dateTimePicker2.Enabled = value;
            dateTimePicker3.Enabled = value;
        }

        private void button18_Click(object sender, EventArgs e)
        {
            this.euro.Date = dateTimePicker1.Value.Date;
            this.euro.Amount = Decimal.Parse(this.euroTxt.Text);
            DataManager.Instance.UpdateEuro(this.euro);
        }

        private void button19_Click(object sender, EventArgs e)
        {
            try
            {
                this.exchangeService.AuthenticationHeaderValue = this.aHeader;
                DataSet exchange = this.exchangeService.GetCurrentExchangeRates(3);
                DataTable tbl = exchange.Tables[0];
                this.euroTxt.Text = tbl.Rows[0]["MiddleRate"].ToString();
            }
            catch (Exception ex)
            {
                LogManager.LogException("Unable to retreive euro course", ex);
            }
        }
    }
}